package linked.base;

public class BaseDemo2 {

    public ListNode createList1(int[] arr) {
        ListNode head = null;
        for (int i : arr) {
            ListNode tmpNode = new ListNode(i);
            tmpNode.next = head;
            head = tmpNode;
        }
        return head;
    }

    public ListNode createList2(int[] arr) {
        ListNode dummy = new ListNode(0, null);
        for (int i : arr) {
            ListNode tmpNode = new ListNode(i);
            tmpNode.next = dummy.next;
            dummy.next = tmpNode;
        }
        return dummy.next;
    }

    public ListNode createList3(int[] arr) {
        ListNode head = new ListNode(arr[0]);
        ListNode tail = head;
        for (int i = 1; i < arr.length; i++) {
            ListNode tmpNode = new ListNode(arr[i]);
            tail.next = tmpNode;
            tail = tail.next;
        }
        return head;
    }

    public ListNode createList4(int[] arr) {
        ListNode dummy = new ListNode(0, null);
        ListNode tail = dummy;
        for (int i = 0; i < arr.length; i++) {
            tail.next = new ListNode(arr[i]);
            tail = tail.next;
        }
        return dummy.next;
    }

    public void printList(ListNode root) {
        while (root != null) {
            System.out.print(root.val + " ");
            root = root.next;
        }
        System.out.println();
        System.out.println("------------------------------");
    }

    public static void main(String[] args) {
        BaseDemo2 baseDemo2 = new BaseDemo2();
        int[] arr1 = new int[]{1, 2, 3, 4, 5};
        ListNode list1 = baseDemo2.createList1(arr1);
        baseDemo2.printList(list1);

        int[] arr2 = new int[]{1, 2, 3, 4, 5};
        ListNode list2 = baseDemo2.createList2(arr2);
        baseDemo2.printList(list2);

        int[] arr3 = new int[]{1, 2, 3, 4, 5};
        ListNode list3 = baseDemo2.createList3(arr3);
        baseDemo2.printList(list3);

        int[] arr4 = new int[]{1, 2, 3, 4, 5};
        ListNode list4 = baseDemo2.createList4(arr4);
        baseDemo2.printList(list4);
    }
}
